//
//  GSDKUserSwitchAccuntView.m
//  GameSDK
//
//  Created by Johnny on 2017/9/8.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKUserSwitchAccuntView.h"
#import "GSDKCommonMethod.h"
#import "GSDKUserConfig.h"
#import "YYKit.h"
#import "WXApi.h"

@interface GSDKUserSwitchAccuntView ()
@property (nonatomic,retain) UIButton * cancelButton;

@property (nonatomic,retain) UIImageView * logoImageView;
@property (nonatomic,retain) UIButton * loginButton;
@property (nonatomic,retain) UIButton * signupButton;
@property (nonatomic,retain) UIButton * fastSignupButton;
@property (nonatomic,retain) UIButton * serviceButton;
@property (nonatomic,strong) UIButton * backButton;
@end

@implementation GSDKUserSwitchAccuntView
- (void)dealloc {
    DEBUGMSG(@"GSDKUserSwitchAccuntView dealloc");
}

- (id)init {
    self = [super init];
    if (self != nil) {
        [self setupUI];
        
        if (GSDKCOMMONMETHOD.accountModel != nil) {
            _backButton.hidden = NO;
        } else {
            _backButton.hidden = YES;
        }
    }
    return self;
}

#pragma mark - setupUI
- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 4;
    
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setTitle:[NSString stringWithFormat:@" %@", [NSString localizedStringForKey:@"GSDK_back_text" value:@"返回"]] forState:UIControlStateNormal];
    [_backButton setTitleColor:[UIColor colorWithHexString:@"#b9b9b9"] forState:UIControlStateNormal];
    _backButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [_backButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/left"] forState:UIControlStateNormal];
    _backButton.frame = CGRectMake(10, 8, 50, 30);
    [self addSubview:_backButton];
    
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_cancelButton setImage:[UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/close"] forState:UIControlStateNormal];
    _cancelButton.frame = CGRectMake(UserViewWidth - 40, 10, 30, 30);
    [self addSubview:_cancelButton];
    
    _logoImageView = [[UIImageView alloc]init];
    _logoImageView.image = [UIImage imageNamed:@"ZYSDK_Resourcres.bundle/pic/newLogo"];
    _logoImageView.frame = CGRectMake(UserViewWidth/2 - 119.7/2, 15, 119.7, 35);
    [self addSubview:_logoImageView];
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if (UserViewWidth < 320) {
        _loginButton.frame = CGRectMake(10, _logoImageView.frame.size.height + _logoImageView.frame.origin.y + 30, UserViewWidth - 10*2, 36);
    } else {
        _loginButton.frame = CGRectMake(30, _logoImageView.frame.size.height + _logoImageView.frame.origin.y + 30, UserViewWidth - 30*2, 36);
    }
    
    _loginButton.backgroundColor = [UIColor colorWithHexString:@"#0197d2"];
    _loginButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
    [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_loginButton setTitle:[NSString stringWithFormat:@"%@", [NSString localizedStringForKey:@"GSDK_login_text" value:@"登录"]] forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_loginButton];
    _loginButton.layer.cornerRadius = 2;
    
    _signupButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _signupButton.frame = CGRectMake(_loginButton.frame.origin.x, _loginButton.frame.size.height + _loginButton.frame.origin.y + 10, _loginButton.frame.size.width, 36);
    _signupButton.backgroundColor = [UIColor colorWithHexString:@"#0197d2"];
    _signupButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
    [_signupButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_signupButton setTitle:[NSString stringWithFormat:@"%@", [NSString localizedStringForKey:@"GSDK_registeraccount_text" value:@"注册账号"]] forState:UIControlStateNormal];
    [_signupButton addTarget:self action:@selector(signupAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_signupButton];
    _signupButton.layer.cornerRadius = 2;
    
    _fastSignupButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _fastSignupButton.frame = CGRectMake(_signupButton.frame.origin.x, _signupButton.frame.size.height + _signupButton.frame.origin.y + 10, _signupButton.frame.size.width, 36);
    _fastSignupButton.backgroundColor = [UIColor colorWithHexString:@"#0197d2"];
    _fastSignupButton.titleLabel.font = [UIFont systemFontOfSize:14.5];
    [_fastSignupButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_fastSignupButton setTitle:[NSString stringWithFormat:@"%@", [NSString localizedStringForKey:@"GSDK_quickregistration_text" value:@"一秒注册"]] forState:UIControlStateNormal];
    [_fastSignupButton addTarget:self action:@selector(fastSignupAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_fastSignupButton];
    _fastSignupButton.layer.cornerRadius = 2;
    
    NSMutableArray * array = [NSMutableArray new];
    for (GSDKResTypeModel * model in GSDKCOMMONMETHOD.menuModel.type) {
        if ([model.type isEqualToString:@"6"]) {
            if (GSDKCOMMONMETHOD.WXAppId == nil) {
                continue;
            }
            if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
                continue;
            }
        }
        [array appendObject:model];
    }
    
    if ([array count] > 0) {
        NSString * phone = [NSString localizedStringForKey:@"GSDK_quicklogin_text" value:@"快捷登录 >"];
        CGSize phoneSize = [phone sizeWithFont:[UIFont systemFontOfSize:13]
                             constrainedToSize:CGSizeMake(UserViewWidth, 24) lineBreakMode:NSLineBreakByWordWrapping];
        
        _serviceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _serviceButton.backgroundColor = [UIColor colorWithHexString:@"#d4d4d4"];
        _serviceButton.frame = CGRectMake(_fastSignupButton.frame.origin.x, _fastSignupButton.frame.origin.y + _fastSignupButton.frame.size.height + 10, phoneSize.width + 20, 28);
        _serviceButton.titleLabel.font = [UIFont systemFontOfSize:13];
        
        [_serviceButton setTitle:phone forState:UIControlStateNormal];
        [self addSubview:_serviceButton];
        _serviceButton.layer.cornerRadius = 14;
        
        int i = 0;
        for (GSDKResTypeModel * model in array) {
            UIButton * thirdPartyButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [thirdPartyButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"ZYSDK_Resourcres.bundle/pic/%@", model.icon]] forState:UIControlStateNormal];
            thirdPartyButton.layer.cornerRadius = 14;
            thirdPartyButton.layer.masksToBounds = true;
            thirdPartyButton.tag = i;
            thirdPartyButton.frame = CGRectMake(_serviceButton.frame.origin.x + _serviceButton.frame.size.width + 16 + (16 * i) + (28 * i) , _serviceButton.frame.origin.y, 28, 28);
            [self addSubview:thirdPartyButton];
            
            [thirdPartyButton addTarget:self action:@selector(thirdPartyAction:) forControlEvents:UIControlEventTouchUpInside];
            i = i + 1;
        }
    }
}

#pragma mark - Action
- (void)cancelAction {
    [[NSNotificationCenter defaultCenter] postNotificationName:GSDKUserCancelNoticationName object:nil];
}

- (void)backAction {
    if (_backBlock) {
        _backBlock();
    }
}

- (void)loginAction {
    if (_loginBlock) {
        _loginBlock();
    }
}

- (void)signupAction {
    if (_signupBlock) {
        _signupBlock();
    }
}

- (void)fastSignupAction {
    if (_fastSignupBlock) {
        _fastSignupBlock();
    }
}

- (void)thirdPartyAction:(UIButton *)button {
    GSDKResTypeModel * model = GSDKCOMMONMETHOD.menuModel.type[button.tag];
    ThirdPartyLoginType type = (ThirdPartyLoginType)([model.type intValue]);
    if (_thirdPartyBlock) {
        _thirdPartyBlock(type);
    }
}
@end
